import numpy as np
import pandas as pd
import logging
import warnings
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder, StandardScaler, OneHotEncoder
from sklearn.metrics import confusion_matrix, classification_report, accuracy_score
from train import process_data,build_cnn_tcn_transformer_classification

warnings.filterwarnings('ignore')

# 日志配置
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

# 配置项
FILE_PATH = r"C:\Users\feng1\Desktop\广东-02.csv"
TEXT_FEATURE_COLS = ['风向']
NUMERIC_FEATURE_COLS = ['温度℃', '降水量(mm)', '风力(级)', '风速(km/h)', '风向角度(度)', '气压(hPa)',
                        '湿度(%)', '空气质量', '能见度(km)', '云量%', '露点℃', '短波辐射W/m²',
                        '直接辐射W/m²', '散射辐射W/m²', '直接正常辐照度W/m²']
LABEL_COL = '天气'
TIME_STEP = 8
TEST_SIZE = 0.2


processed_data = process_data()
train_X, train_y = processed_data['train_X'], processed_data['train_y']
test_X, test_y = processed_data['test_X'], processed_data['test_y']
n_classes = len(processed_data['class_mapping'])
class_names = [processed_data['class_mapping'][i] for i in range(n_classes)]

# 标签独热编码
oh_encoder = OneHotEncoder(sparse_output=False)
train_y_onehot = oh_encoder.fit_transform(train_y.reshape(-1, 1))
test_y_onehot = oh_encoder.transform(test_y.reshape(-1, 1))

# 模型构建
input_shape = (train_X.shape[1], train_X.shape[2])
time_steps = train_X.shape[1]
model = build_cnn_tcn_transformer_classification(
    input_shape=input_shape,
    n_classes=n_classes,
    time_steps=time_steps
)

# 加载权重
try:
    model.load_weights('final_model.weights.h5')
    print("成功加载模型权重")
except:
    print("未找到权重文件")

# 评估
test_pred_proba = model.predict(test_X)
test_pred_class = np.argmax(test_pred_proba, axis=1)


# 输出精度指标
print(f"\n测试集准确率: {accuracy_score(test_y, test_pred_class):.4f}")
print("\n分类报告:")
print(classification_report(test_y, test_pred_class, target_names=class_names))

#- accuracy: 0.7410 - loss: 0.6302 - val_accuracy: 0.6306 - val_loss: 0.9844 - learning_rate: 2.8248e-05
